package com.cxx.autoconfigure.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.FieldError;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class WebUtils {
    public static boolean checkIsAuditPackages(Package currentPackage, String basePackages) {
        return StringUtils.startsWith(currentPackage.getName(), basePackages);
    }

    public static String formatValidationException(List<FieldError> fieldErrorList) {
        StringBuilder message = new StringBuilder();
        fieldErrorList.forEach(fieldError ->
                message.append(fieldError.getField())
                        .append(": ")
                        .append(fieldError.getDefaultMessage())
                        .append("; ")
        );

        return message.toString();
    }

    public static String format2UTF8(String string) {
        return URLDecoder.decode(string, StandardCharsets.UTF_8);
    }
}
